`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    18:32:28 03/27/2014 
// Design Name: 
// Module Name:    gamelogic 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module gamelogic( 	clk,
							up,
							down,
							reset,
							enemy1_generation,
							enemy2_generation,
							bullet_generation,
							game_over,
							lives,
							a,
							b,
							c,
							d,
						
							xpos_central, 
							ypos_central, 
							xpos_enemy1, 
							ypos_enemy1, 
							xpos_enemy2, 
							ypos_enemy2,
							xpos_bullet, 
							ypos_bullet, 
							enemy_signal1, 
							enemy_signal2, 
							bullet_signal,
							collision
				
							);


input up,down;
input clk;
input enemy1_generation, //switch for enemy1 generation
		enemy2_generation, //switch for enemy2 generation
		bullet_generation; //switch for bullet generation
input reset;//switch for reset


output reg[9:0] xpos_central, ypos_central;
output reg[3:0] lives;
output reg game_over;
output [3:0] a,b,c,d;
output reg[9:0] xpos_enemy1, ypos_enemy1;
output reg[9:0] xpos_enemy2, ypos_enemy2;
output reg[9:0] xpos_bullet, ypos_bullet;
output reg 	enemy_signal1,//enemy1 generation signal
				enemy_signal2, //enemy2 generation signal
				bullet_signal;
//output reg start;

reg [15:0] scores;
wire [9:0] position_x1,position_y1;
wire [9:0] position_x2,position_y2;
wire [9:0] position_x3,position_y3;
wire position_valid1,position_valid2,position_valid3;
output reg collision;

reg [20:0] freq_count_y;
reg [40:0] freq_count_z;

initial begin
	//xpos_central <= 0;
	//ypos_central <= 0;
	//scores<=16'b0000000000000000;
	//lives<=4'b0011;
	//game_over<=0;
	//enemy_signal1<=0;
	//enemy_signal2<=0;
	//bullet_signal<=0;
	//start<=1;
	//freq_count_y <=0;
	//freq_count_z <=0;
	collision<=0;
	game_over<=0;
	//start<=0;
end

position_generation	pos_generation(	.clk(clk),
													.new_enemy_1(enemy1_generation),//enemy1 generation signal
													.new_enemy_2(enemy2_generation),//enemy2 generation signal
													.new_bullet_1(bullet_generation),
													.cha_pos_x(xpos_central),//character position signal
													.cha_pos_y(ypos_central),
													.position_x_1(position_x1),
													.position_y_1(position_y1),
													.position_x_2(position_x2),
													.position_y_2(position_y2),
													.position_bullet_x(position_x3),
													.position_bullet_y(position_y3),
													.position_valid_1(position_valid1), //if enemy is in the right range
													.position_valid_2(position_valid2), //if enemy is in the right range
													.position_valid_3(position_valid3));//if bullet is in the right range

scores_dec score ( scores,a,b,c,d);
//vga vga_1(	.clk_old(clk), 
//				.enable1(enemy_signal1),
//				.enable2(enemy_signal2), 
//				.enable3(bullet_signal), 
//				.red(red),
//				.green(green),
//				.blue(blue),
//				.HS(HS),
//				.VS(VS),
//				.X1(xpos_enemy1),
//				.Y1(ypos_enemy1),
//				.X2(xpos_enemy2),
//				.Y2(ypos_enemy2),
//				.X3(xpos_bullet),
//				.Y3(ypos_bullet),
//				.X4(xpos_central),
//				.Y4(ypos_central));

				
always@ (posedge clk) 
begin
	if(reset)
	begin
			xpos_central 	<= 10;
			ypos_central 	<= 240;
			scores			<= 16'b0000000000000000;
			lives				<=4'b0011;
			game_over		<=0;
			enemy_signal1	<=0;
			enemy_signal2	<=0;
			bullet_signal	<=0;
			//start				<=1;
			freq_count_y 	<=0;
			freq_count_z <=0;
			collision<=0;
	end 
	else
	begin
	//if(start==0)
	//start<=1;
	//else
	//start=0;
	
	   collision<=0;	
		if(enemy1_generation) 
		begin
			if(position_valid1) 
			begin
				xpos_enemy1 <= position_x1; 
				ypos_enemy1 <= position_y1;  
				collision<=0;
			end
			else 
			begin
				enemy_signal1 <= 1;
			end
		end

		if(enemy2_generation) 
		begin
			if(position_valid2) 
			begin
				xpos_enemy2 <= position_x2; 
				ypos_enemy2 <= position_y2;  
				collision<=0;
			end
			else 
			begin
				enemy_signal2<=1;
			end
		end
		
		if(bullet_generation) 
		begin
			if(position_valid3) 
			begin
				 xpos_bullet <= position_x3;
				 ypos_bullet <= position_y3; 
				 collision<=0;
			end
			else 
			begin
				bullet_signal<=1;
			end
		end
		//scoreboard
		
		
		//central character control
		
		if (freq_count_y == 1680000)
		begin
			freq_count_y <= 0;
			collision<=0;
		end
		else
			freq_count_y <= freq_count_y + 1;
	
		
		if (freq_count_z == 100000000)
		begin
			freq_count_z <= 0;
			collision<=0;
		end
		else
			freq_count_z <= freq_count_z + 1;
	
		
		
		
		if (up==1 & freq_count_y ==0)
			ypos_central <= ypos_central + 1; 
		
		if (down==1 & freq_count_y ==0)
			ypos_central <= ypos_central - 1; 
			
		if(ypos_central == 480) begin
			ypos_central <= 100; end
			
		else if(ypos_central < 100) begin
			ypos_central <= 440;		 end
	
		if(freq_count_z==0)
			scores <= scores + 16'b0000000000001010;
			
		if(lives<1)
			game_over<=1;
		
		if(scores<0)
			scores = 0;
		
		
	if((39<= xpos_enemy1 - xpos_central <= 40 && 39 <= ypos_enemy1 - ypos_central <= 40 ) || 
	  (39<= xpos_enemy2 - xpos_central <= 40 && 39 <= ypos_enemy2 - ypos_central <= 40 ) ||
	  (xpos_bullet - xpos_central <= 20 && ypos_bullet - ypos_central <= 20 ))
	begin
	xpos_central <= 30;
	ypos_central <= 240;
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	//enemy_signal1<=1;
	//enemy_signal2<=1;
	//bullet_signal<=1;
   end
	
	//else 
	//collision<=0;
	//	if(up)
	//		ypos_central = ypos_central + 1;
	//	else if(down)
	//		ypos_central = ypos_central - 1;
	//	else 
	//		ypos_central = ypos_central;
	
	
		end	
end
	


endmodule
